package com.tencent.sr.iris.activity.common.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.tencent.sr.rmall.common.exception.TMallBizException;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Set;
import java.util.stream.Collectors;

public class ValidateUtils {

    public static void validate(Object obj, Class<?>... groups) {
        Validator validator = SpringUtil.getBean(Validator.class);
        Set<ConstraintViolation<Object>> violations = validator.validate(obj, groups);
        if (CollUtil.isNotEmpty(violations)) {
            String message = violations.stream().map(ConstraintViolation::getMessage).collect(Collectors.joining("|"));
            throw new TMallBizException(message);
        }
    }
}
